#!/bin/sh
# 
# Automate updating package version metadata on bintray.com.
# This script should only be run run *after* binary uploads.
#

# Run in the top-level of a tree, ensuring .bintrayrc is available with
# valid user setting (username, email, apikey, distro, gpgphrase).

topdir=`pwd`

quit()
{
    echo $*
    exit 1
}

[ -e "${topdir}/VERSION.pcp" ] || quit "Not a PCP git tree, missing VERSION.pcp"
[ -e "${topdir}/.bintrayrc" ] || quit "Tree is unconfigured, missing .bintrayrc"

. ${topdir}/.bintrayrc
. ${topdir}/VERSION.pcp

version=${PACKAGE_MAJOR}.${PACKAGE_MINOR}.${PACKAGE_REVISION}
date=`date "+%Y-%m-%dT00:00:00.000Z"`

[ -z "${user}" ] && quit "user is not configured (via .bintrayrc)"
[ -z "${email}" ] && quit "email is not configured (via .bintrayrc)"
[ -z "${apikey}" ] && quit "apikey is not configured (via .bintrayrc)"
[ -z "${gpgpass}" ] && quit "passphrase is not configured (via .bintrayrc)"

version_update()
{
    distro="$1"; desc=`lookup_description "$1"`

    url="https://api.bintray.com/packages/pcp/${distro}/pcp/versions/${version}"
    echo "Updating ${distro} information for ${version}:" && echo "    ${url}"
    curl \
	-u ${user}:${apikey} \
	-H "X-GPG-PASSPHRASE: ${gpgpass}" \
	-H "Content-Type:application/json" \
	-H "Accept: application/json" \
	--request PATCH \
	--data "{\"desc\":\"$desc\",\"vcs_tag\":\"$version\",\"released\":\"$date\"}" \
	"${url};bt_package=pcp;bt_version=${version};publish=1"
    echo
}

lookup_description()
{
    distro="$1"

    awk </dev/null '
	BEGIN {
table["source"] = "Performance Co-Pilot source code, gzipped tarball"
table["el5"] = "Performance Co-Pilot builds for EL5"
table["el6"] = "Performance Co-Pilot builds for EL6"
table["el7"] = "Performance Co-Pilot builds for EL7"
table["el8"] = "Performance Co-Pilot builds for EL8"
table["f24"] = "Performance Co-Pilot builds for Fedora 24"
table["f25"] = "Performance Co-Pilot builds for Fedora 25"
table["f26"] = "Performance Co-Pilot builds for Fedora 26"
table["f27"] = "Performance Co-Pilot builds for Fedora 27"
table["f28"] = "Performance Co-Pilot builds for Fedora 28"
table["f29"] = "Performance Co-Pilot builds for Fedora 29"
table["f30"] = "Performance Co-Pilot builds for Fedora 30"
table["f31"] = "Performance Co-Pilot builds for Fedora 31"
table["jessie"] = "Performance Co-Pilot builds for Debian 7 (jessie)"
table["wheezy"] = "Performance Co-Pilot builds for Debian 8 (wheezy)"
table["stretch"] = "Performance Co-Pilot builds for Debian 9 (stretch)"
table["buster"] = "Performance Co-Pilot builds for Debian 10 (buster)"
table["trusty"] = "Performance Co-Pilot builds for Ubuntu 14.04 (trusty tahr)"
table["vivid"] = "Performance Co-Pilot builds for Ubuntu 15.04 (vivid vervet)"
table["xenial"] = "Performance Co-Pilot builds for Ubuntu 16.04 (xenial xerus)"
table["zesty"] = "Performance Co-Pilot builds for Ubuntu 17.04 (zesty zapus)"
table["bionic"] = "Performance Co-Pilot builds for Ubuntu 18.04 (bionic beaver)"
table["disco"] = "Performance Co-Pilot builds for Ubuntu 19.04 (disco dingo)"
table["macosx"] = "Performance Co-Pilot builds for Mac OS X"
table["opensuse13"] = "Performance Co-Pilot builds for OpenSUSE 13"
table["opensuse14"] = "Performance Co-Pilot builds for OpenSUSE 14"
table["opensuse15"] = "Performance Co-Pilot builds for OpenSUSE 15"
table["opensuseleap"] = "Performance Co-Pilot builds for OpenSUSE Leap"
table["solaris11"] = "Performance Co-Pilot builds for Solaris 11"
	} END {
	    print(table["'$distro'"])
	}'
}

debian="jessie wheezy stretch"
ubuntu="trusty vivid xenial zesty bionic disco"
macosx="macosx"
redhat="el5 el6 el7 el8 f24 f25 f26 f27 f28 f29 f30"
solaris="solaris11"
suse="opensuse13 opensuse14 opensuse15 opensuseleap"
testing="f29-testing el7-testing"

distributions="source $redhat $debian $ubuntu $macosx $solaris"
[ $# -ge 1 ] && distributions="$@"

for distro in $distributions
do
    version_update $distro
done
